package initialize

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"

	"mxshop-api/oss-web/api"
	"mxshop-api/oss-web/global"
	"mxshop-api/oss-web/router"
	"mxshop-api/shared/auth"
)

func Routers() *gin.Engine {
	Router := gin.Default()
	Router.GET("/health", api.Health)

	router.InitUploadRouter(Router) // 注册上传静态页面

	// 配置跨域
	config := cors.DefaultConfig()
	if len(global.ServerConfig.CorsConfig.AllowOrigins) == 0 {
		config.AllowAllOrigins = true
	} else {
		config.AllowOrigins = global.ServerConfig.CorsConfig.AllowOrigins
	}
	config.AllowHeaders = append(config.AllowHeaders, auth.AuthorizationHeader) // 允许修改认证信息的Header
	Router.Use(cors.New(config))

	ApiGroup := Router.Group("/oss/v1")
	router.InitOSSRouter(ApiGroup)

	return Router
}
